package file;

import java.io.*;
import javax.swing.JFileChooser;
import org.apache.commons.io.IOUtils;
import ctrl.Control;

public class FileShare {

	public static String filename, filename2, filename1,filenamesaved;
	int mode = 0;
	Control gui;

	public FileShare(Control g) {
		gui = g;
	}

	public void action() {
		gui.println("System::Selecting file to send.");
		(new Thread(new sendFile())).start();
	}

	private class sendFile implements Runnable {
		public void run() {
			JFileChooser fileDialog = new JFileChooser();
			int openChoice = fileDialog.showOpenDialog(null);
			if (openChoice == JFileChooser.APPROVE_OPTION) {
				File selectedFile = fileDialog.getSelectedFile();
				filename1 = selectedFile.getName();
				gui.println("System::You are broadcasting "
						+ filename1 + " to others.");
				gui.getNet()
						.sendString(
								"Receiving "
										+ filename1
										+ " from "
										+ gui.getUsr().getName()
										+ ". Please choose a location and name to save it or press cancel to deny the file.",
								1);
				gui.println("System::Sending file " + filename1
						+ " of size (bytes) " + selectedFile.length());
				byte[] data;
				byte[] filenameforsaving;
				filenameforsaving = filename1.getBytes();
				try {
					data = org.apache.commons.io.FileUtils.readFileToByteArray(selectedFile);
					gui.getNet().sendBytes(filenameforsaving, 7);// pass the name of the sending file
					gui.getNet().sendBytes(data, 3);
					gui.println("System::Transmission completed");
				} catch (IOException e) {
					System.out.println("@FileShare - IOException: FileSh Error: file error");
				}


			}
		}
	}

	public void receiveFile(byte[] data) {
		(new Thread(new saveFile(data))).start();
	}

	private class saveFile implements Runnable {
		private byte[] data;

		public saveFile(byte[] input) {
			data = input;
		}

		public void run() {
			try {
				JFileChooser fileDialog = new JFileChooser();
				fileDialog.setSelectedFile(new File(filenamesaved));
				int saveChoice = fileDialog.showSaveDialog(null);
				if (saveChoice != JFileChooser.CANCEL_OPTION) {
					File file = fileDialog.getSelectedFile();
					FileOutputStream fos = new FileOutputStream(file.getPath());
					fos.write(data);
					fos.close();
					gui.println("System::File saved.");
				} else
					gui.println("System::Reception cancelled.");
			} catch (Exception e) {
				gui.println("@FileShare - Exception: System::Error while saving file");
			}

		}
	}
	public void receiveFilename(byte[] name) {
		(new Thread(new saveFilename(name))).start();
	}
	private class saveFilename implements Runnable {
		private byte[] data;

		public saveFilename(byte[] input) {
			data = input;
		}

		public void run() {
			InputStream in = new ByteArrayInputStream(data);
			try {
				filenamesaved = IOUtils.toString(in, "UTF-8");
			} catch (IOException e) {
				gui.println("@FileShare - Exception: System::Error while saving filename");
			}
		}
	}}
